package  com.diamond.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diamond.bo.ClientOrderBO;
import com.diamond.bo.ClientOrderDetailBO;
import com.diamond.bo.QueryOrderBO;
import com.diamond.bo.ReturnBO;
import com.diamond.entity.Clientorder;
import com.diamond.entity.Clientorderdetail;
import com.diamond.facade.ClientorderdetailFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.JsonUtils;
import com.diamond.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * OrderdetailAction ������
 * @author JSmart Tools
 */
public class ClientorderdetailAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private static Logger log=LoggerFactory.getLogger(ClientorderdetailAction.class);
	private HttpServletRequest request;
	private ClientorderdetailFacade clientorderdetailFacade;
	private ClientOrderDetailBO clientOrderDetailBo;
	private QueryOrderBO queryOrderBo;
	private List<ClientOrderDetailBO> dataList;
	private String json;
	private String tips;
	
	/**
	 * 代理商查看未处理钻托订单
	 */
	public String toCenInlayList(){
		String url="toceninlaylist.action?a=1";
		cenInlayList(0,url);
		return SUCCESS;
	}
	/**
	 * 代理商查看已处理钻托订单
	 */
	public String toDelCenInlayList(){
		String url="todelceninlaylist.action?a=1";
		cenInlayList(1,url);
		return SUCCESS;
	}
	public String cenInlayList(int type,String url){
		Map session = ActionContext.getContext().getSession(); 
	    Object obj=session.get("USER_ID");
	    if(obj==null)return SUCCESS;
	    try{
	    	Long id=(Long) obj;
	    	
	    	String HQL="from Clientorderdetail where inlayUserId="+id+" and state="+type+" order by orderDate desc";
	    	List list = Page.util(ServletActionContext.getRequest(),url,clientorderdetailFacade,HQL,20);
	    	if(list!=null&&list.size()>0){
	    		dataList = new ArrayList<ClientOrderDetailBO>();
	    		for(int i=0;i<list.size();i++){
	    			Clientorderdetail clientorderdetail = (Clientorderdetail) list.get(i);
	    			ClientOrderDetailBO dest = new ClientOrderDetailBO();
	    			PropertyUtils.copyProperties(dest, clientorderdetail);
	    			dataList.add(dest);
	    		}
	    	}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return SUCCESS;
	}
	
	/**
	 * 代理商处理钻托订单
	 */
	public String doInlay(){
		request=ServletActionContext.getRequest();
		clientorderdetailFacade.doInlay(request);
		return SUCCESS;
	}
	
	public String doSave() {
		try {
			clientorderdetailFacade.save(clientOrderDetailBo);
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}
    
	public String toUpdate() {
		try {
			clientOrderDetailBo = (ClientOrderDetailBO) clientorderdetailFacade.findById(request.getParameter("id"));
			return "toUpdate";
		} catch (Exception e) {
			return ERROR;
		}
	}  

	public String doUpdate() {
		try {
			clientorderdetailFacade.update(clientOrderDetailBo);
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}
	
	/**
	 * 后台查询客户交易明细
	 * @return
	 */
	public String queryClientOrderDetail() {
//		System.out.println("**********************客户订单明细查询*********************");
		request = ServletActionContext.getRequest();
		String start = request.getParameter("start");
		String pageSize = request.getParameter("limit");
		try {
			if(null==json|| "".equals(json.trim())||"null".equals(json.toLowerCase())){//默认查询全部;
				queryOrderBo=new QueryOrderBO();
			}
			else{
				queryOrderBo=(QueryOrderBO) JsonUtils.toObject(json, QueryOrderBO.class);
				if ("-1".equals(queryOrderBo.getState())) {
					queryOrderBo.setState("");
				}
			}
			if (null == start || "".equals(start) || null == pageSize || "".equals(pageSize)) {
				queryOrderBo.setStart(0);
				queryOrderBo.setPageSize(15);
			} else {
				queryOrderBo.setStart(Integer.valueOf(start));
				queryOrderBo.setPageSize(Integer.valueOf(pageSize));
			}
			ReturnBO<ClientOrderDetailBO> returnBo = clientorderdetailFacade.queryClientOrderDetail(queryOrderBo);
			this.json=JsonMSG.sucess(returnBo).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("查询失败").toJson();
			log.error("查询异常!",e);
		}
		return "json";
	}
	
	/**
	 * 修改更新商品的处理状态
	 * @return
	 */
	public String updateClientOrderDetailState() {
//		System.out.println("**********************修改客户订单明细的处理状态*********************");
		request = ServletActionContext.getRequest();
		String cliOrdMasterId = request.getParameter("cliOrdMasterId");
		List list = (List) JsonUtils.toCollection(json, Map.class);
		try {
			if (null != cliOrdMasterId && !"".equals(cliOrdMasterId) && list.size() > 0) {
				Boolean result = clientorderdetailFacade.updateClientOrderDetailState(cliOrdMasterId, list);
				if (result) {
					this.json=JsonMSG.sucess("修改成功!").toJson();
				} else {
					this.json=JsonMSG.failed("修改失败").toJson();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("修改失败").toJson();
			log.error("更新异常!",e);
		}
		return "json";
	}
	
		
	public QueryOrderBO getQueryOrderBo() {
		return queryOrderBo;
	}

	public void setQueryOrderBo(QueryOrderBO queryOrderBo) {
		this.queryOrderBo = queryOrderBo;
	}

	public ClientOrderDetailBO getClientOrderDetailBo() {
		return clientOrderDetailBo;
	}

	public void setClientOrderDetailBo(ClientOrderDetailBO clientOrderDetailBo) {
		this.clientOrderDetailBo = clientOrderDetailBo;
	}

	public List<ClientOrderDetailBO> getDataList() {
		return dataList;
	}

	public void setDataList(List<ClientOrderDetailBO> dataList) {
		this.dataList = dataList;
	}

	public void setClientorderdetailFacade(
			ClientorderdetailFacade clientorderdetailFacade) {
		this.clientorderdetailFacade = clientorderdetailFacade;
	}

	public ClientOrderDetailBO getOrderdetailBo() {
		return clientOrderDetailBo;
	}

	public void setOrderdetailBo(ClientOrderDetailBO orderdetailBo) {
		this.clientOrderDetailBo = orderdetailBo;
	}

	public void setOrderdetailFacade(ClientorderdetailFacade orderdetailFacade) {
		this.clientorderdetailFacade = orderdetailFacade;
	}

	public ClientOrderDetailBO getOrderdetail() {
		return clientOrderDetailBo;
	}

	public void setOrderdetail(ClientOrderDetailBO orderdetailBo) {
		this.clientOrderDetailBo = orderdetailBo;
	}
    
	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}
}
